package com.cn.ZhiKeDianShang.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.cn.ZhiKeDianShang.AppConfig;
import com.cn.ZhiKeDianShang.MainActivity;
import com.cn.ZhiKeDianShang.R;
import com.cn.ZhiKeDianShang.base.BaseActivity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.LoginUserPresenter;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;
import com.cn.ZhiKeDianShang.util.PrefShare;
import com.cn.ZhiKeDianShang.util.ToastUtil;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.HttpUtils;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.ObjectResult;
import com.cn.ZhiKeDianShang.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/7/3.
 * do what to 2017/7/3
 */

public class EmptyActivity extends BaseActivity<LoginUserView, LoginUserPresenter> implements LoginUserView {

    @Override
    protected int getNormalLayoutId() {
        return R.layout.activity_update_info;
    }

    @Override
    protected int getErrorLayoutId() {
        return R.layout.activity_update_info;
    }

    @Override
    protected LoginUserPresenter createPresenter() {
        return new LoginUserPresenter(this, this);
    }

    @Override
    protected void initViewsAndEvents() {
    }


    /**
     * @param
     */
    private void login(String account, String password) {
//        DialogHelper
        Map<String, String> params = new HashMap<>();
        params.put("account", account);          // 账号登陆的时候需要MD5加密，服务器需求
        params.put("password", password);// 账号登陆的时候需要MD5加密，服务器需求
        HttpUtils.post().url(AppConfig.USER_LOGIN)
                .params(params)
                .build()
                .execute(new BaseCallback<LoginRegisterResult>(LoginRegisterResult.class) {
                    @Override
                    public void onResponse(ObjectResult<LoginRegisterResult> result) {
//                        DialogHelper.dismissProgressDialog();
                        Log.d("Debug", "到达请求这里---");
                        if (Result.checkSuccess(EmptyActivity.this, result)) {
                            ToastUtil.showShort("登陆成功");
                            LoginRegisterResult data = result.getData();
                            PrefShare.getInstance(EmptyActivity.this).saveString("token", data.getUserinfo().getToken());
                            PrefShare.getInstance(EmptyActivity.this).saveString("userId", data.getUserinfo().getId()+"");
                            startActivity(new Intent(EmptyActivity.this, MainActivity.class));
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
//                        DialogHelper.dismissProgressDialog();
//                        ToastUtil.showErrorNet(LoginActivity.this);
                    }
                });
    }


    @Override
    public void onLogin(UserInfoBean resultBean) {

    }

    @Override
    public void requestFailed(String message) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
